package mdm.led.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.Constant;

import mdm.led.entity.three.FindThreeTaskSelf;
import mdm.led.entity.three.ThreeMainList;
import mdm.led.entity.three.ThreeTable;
import mdm.led.entity.three.ThreeTaskSelf;
import mdm.led.entity.three.UpdateThreeTableWeb;
import mdm.led.service.ThreeTableService;

/**
 *
 * @author :Monarch丿He
 * @date :2018年12月13日
 * @explain :
 */
@Controller
@RequestMapping(value = "threeTableWeb")
@Api(description = "督导三查Web")
public class ThreeTableWebController {

	private Logger logger = Logger.getLogger(ThreeTableWebController.class);

	@Autowired
	private ThreeTableService threeTableService;

	@ApiOperation(value = "添加三查表", notes = "返回值:<br>100 成功 <br>101:失败 ")
	@RequestMapping(value = "addThreeTable", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse addThreeTable(@RequestBody ThreeTable table) {
		logger.info("\n======== 添加三查表 ========\n");
		boolean flag = false;
		try {
			// 添加外部联系人
			Boolean addThreeTable = threeTableService.addThreeTable(table);
			if (addThreeTable) {
				flag = true;
			}
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, "添加成功");
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "添加失败");
		}
	}

	@ApiOperation(value = "修改三查表", notes = "返回值:<br>100 成功 <br>101:失败 ")
	@RequestMapping(value = "updateThreeTable", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse updateThreeTable(@RequestBody UpdateThreeTableWeb update) {
		logger.info("\n======== 修改三查表 ========\n");
		boolean flag = false;
		try {
			ThreeTable table = new ThreeTable();
			table.setPeriod(update.getPeriod());
			table.setStatus(update.getStatus());
			table.setThreeid(update.getTalbeid());
			// 添加外部联系人
			Integer updateThreeTable = threeTableService.updateThreeTable(table);
			if (updateThreeTable > 0) {
				flag = true;
			}
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, "修改成功");
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "修改失败");
		}
	}

	@ApiOperation("根据id 查询三查表")
	@RequestMapping(value = "findThreeTableById", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResponse findThreeTableById(@RequestParam("tableid") Integer id) {
		logger.info("\n======== 根据id 查询三查表  ========\n");
		boolean flag = false;
		ThreeTable findThreeTableById = null;
		try {
			findThreeTableById = threeTableService.findThreeTableById(id);
			flag = true;
		} catch (Exception e) {
			flag = false;
			logger.info(e);
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(findThreeTableById) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, findThreeTableById);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}

	@ApiOperation(value = "督导三查统计列表", notes = "name : 表格模糊查询")
	@RequestMapping(value = "findThreeMainList", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResponse findThreeMainList(@RequestParam(value = "name", required = false) String name,
			@RequestParam("pageSize") Integer pageSize, @RequestParam("currPage") Integer currPage) {
		logger.info("\n======== 查询三查表  ========\n");
		boolean flag = false;
		// 需要返回的 map 数据集合
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			// 开启分页
			Page<ThreeMainList> page = PageHelper.startPage(currPage, pageSize);
			// 查询外部联系人列表
			List<ThreeMainList> findThreeMainList = threeTableService.findThreeMainList(0, name);
			// 数据
			data.put("list", findThreeMainList);
			// 当前页
			data.put("currPage", page.getPageNum());
			// 页总数
			data.put("pageCount", page.getPages());
			// 页大小
			data.put("pageSize", page.getPageSize());
			// 总条数
			data.put("totalCount", page.getTotal());
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(data) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, data);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}

	@ApiOperation(value = "督导三查详情列表", notes = "未完成状态不能进入")
	@RequestMapping(value = "findThreeTaskSelf", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse findThreeTaskSelf(@RequestBody FindThreeTaskSelf taskSelf,
			@RequestParam("pageSize") Integer pageSize, @RequestParam("currPage") Integer currPage) {
		logger.info("\n======== 查询三查表  ========\n");
		boolean flag = false;
		// 需要返回的 map 数据集合
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			// 开启分页
			Page<ThreeTaskSelf> page = PageHelper.startPage(currPage, pageSize);
			// 查询外部联系人列表
			List<ThreeTaskSelf> ThreeTaskSelf = threeTableService.findThreeTaskSelf(taskSelf);
			// 数据
			data.put("list", ThreeTaskSelf);
			// 当前页
			data.put("currPage", page.getPageNum());
			// 页总数
			data.put("pageCount", page.getPages());
			// 页大小
			data.put("pageSize", page.getPageSize());
			// 总条数
			data.put("totalCount", page.getTotal());
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(data) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, data);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}

	@ApiOperation(value = "根据自查任务ID 查询详情表信息", notes = "selfTask:任务详情  <br> items:任务检查项")
	@RequestMapping(value = "findThreeTaskSelfByID", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResponse findThreeTaskSelfByID(@RequestParam("taskID") Integer id) {
		logger.info("\n======== 根据自查任务ID 查询详情表信息  ========\n");
		boolean flag = false;
		Map<String, Object> map = null;
		try {
			map = threeTableService.findThreeTaskSelfByID(id);
			flag = true;
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(map) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, map);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}

}
